package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @RequestMapping("/create")
    public String create(Long houseId, Model model){
        //将houseId传递到添加经纪人的页面
        model.addAttribute("houseId",houseId);
        //查询所有Admin列表，并放入model
        List<Admin> adminList = this.adminService.findAll();
        model.addAttribute("adminList",adminList);

        return "houseBroker/create";
    }
    @RequestMapping("/save")
    public  String save(HouseBroker houseBroker){
        //根据adminid去查询admin的所有信息
        Admin admin = this.adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        //增加了BrokerName，BrokerHeadUrl后可以添加经纪人信息
        this.houseBrokerService.insert(houseBroker);
        return "common/successPage";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        //显示当前经纪人
        HouseBroker houseBroker = this.houseBrokerService.getById(id); //??
        model.addAttribute("houseBroker",houseBroker);
        //查询所有Admin列表，并放入model
        List<Admin> adminList = this.adminService.findAll();
        model.addAttribute("adminList",adminList);

        return "houseBroker/edit";
    }

    @RequestMapping("/update")
    public  String update(HouseBroker houseBroker){
        //根据adminid去查询admin的所有信息
        Admin admin = this.adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        //增加了BrokerName，BrokerHeadUrl后可以添加经纪人信息
        this.houseBrokerService.update(houseBroker);//???
        return "common/successPage";
    }
}
